﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iLangl
{
	public class Alert : BaseAlert, IAlert
	{
		public Alert(String source)
		{
			_source = source;
			created = DateTime.Now;
		}

		#region IAlert Members




		private IList<IError> _errors = null;
		public IList<IError> Errors
		{
			get
			{
				if (_errors == null)
				{
					_errors = new List<IError>();
				}

				return _errors;
			}
		}

		public void AddError(IError error)
		{
			Errors.Add(error);
		}


		public void AddErrors(IList<IError> errors)
		{
			foreach (IError error in errors)
			{
				Errors.Add(error);
			}
		}

		private IList<IAction> _actions = null;
		public IList<IAction> Actions
		{
			get
			{
				if (_actions == null)
				{
					_actions = new List<IAction>();
				}
				return _actions;
			}
		}

		public void AddAction(IAction action)
		{
			Actions.Add(action);
		}


		private IList<IPackage> _packages = null;
		public IList<IPackage> Packages
		{
			get
			{
				if (_packages == null)
				{
					_packages = new List<IPackage>();
				}

				return _packages;
			}
		}

		public void AddPackage(IPackage package)
		{
			Packages.Add(package);
		}

		private IList<Exception> _exception = null;
		public IList<Exception> Exceptions
		{
			get
			{
				if (_exception == null)
				{
					_exception = new List<Exception>();
				}
				return _exception;
			}
		}




		public void AddException(Exception ex)
		{
			Exceptions.Add(ex);
		}

		#endregion


		public override eSeverity Severity
		{
			get
			{
				eSeverity severity = eSeverity.Unknown;

				foreach (IError error in Errors)
				{
					severity = (eSeverity)Math.Max((int)severity, (int)error.Severity);
				}

				if (Exceptions.Count > 0)
				{
					severity = (eSeverity)Math.Max((int)severity, (int)eSeverity.High);
				}

				return severity;
			}
			set
			{
				Facade.Instance.AlertManager.RaiseAlert(new NotSupportedException());
			}
		}
	}
}